
/**
 * 加载TREEGRID扩展模块
 * 
 * @auth liangpingzheng
 * @date 2018-03-09
 */
layui.extend({
    treeGird: '../layui/lay/modules/treeGird'
});

/**
 *	菜单管理
 *	@auth liangpingzheng
 *	@date 2018-04-27
 */
layui.use(['form', 'tree', 'layer', 'laytpl', 'treeGird'], function () {
    var layer = parent.layer === undefined ? layui.layer : top.layer,
            form = layui.form,
            layedit = layui.layedit,
            laytpl = layui.laytpl,
            treeGird = layui.treeGird,
            $ = layui.$;

    /**
     * 设置表头
     */
    var layout =
            [{name: 'ID',
                    headerClass: 'value_col',
                    colClass: 'value_col',
                    style: 'width: 5%',
                    render: function (row) {
                        return row.id;
                    }
                },
                {
                    name: '菜单名称',
                    treeNodes: true,
                    headerClass: 'value_col',
                    colClass: 'value_col',
                    style: '15%',
                    render: function (row) {
                        return row.title;
                    }
                },
                {
                    name: '权限节点',
                    headerClass: 'value_col',
                    colClass: 'value_col',
                    style: 'width: 60%',
                    render: function (row) {
                        var str = '';
                        if (row.appList != null) {
                            for (var i = 0; i < row.appList.length; i++) {
                                var item = row.appList[i];
                                str += '<input name="auth[' + row.id + ',' + item.id + ']" lay-skin="primary" title="' + item.name + '" ' + (item.selected == 1 ? 'checked=""' : "") + ' type="checkbox">';
                            }
                        }
                        return str;
                    }
                },
                {
                    name: '图标',
                    headerClass: 'value_col',
                    colClass: 'value_col',
                    style: 'width: 5%',
                    render: function (row) {
                        return '<i class="larry-icon ' + row.icon + '" data-icon="' + row.icon + '" data-font="larry-icon"></i>';
                    }
                },
                {
                    name: '排序',
                    headerClass: 'value_col',
                    colClass: 'value_col',
                    style: 'width: 5%',
                    render: function (row) {
                        return row.sort_order;
                    }
                },
                {
                    name: '操作',
                    headerClass: 'value_col',
                    colClass: 'value_col',
                    style: 'width: 10%;text-align:center;',
                    render: function (row) {
                        if (row.appList != null) {
                            if (row.appList.length > 0) {
                                var str = '<a class="layui-btn layui-btn-xs btnEdit" lay-event="edit" data-id="' + row.id + '"><i class="layui-icon">&#xe632;</i>全选</a>';
                                str += '<a class="layui-btn layui-btn-xs btnEdit2" lay-event="edit2" data-id="' + row.id + '"><i class="layui-icon">&#xe632;</i>全不选</a>';
                                return str;

                            }
                        }
                        return '';
                    }
                }];

    //获取类型
    var type = $("#type").val();
    var typeId = $("#type_id").val();

    /**
     * 获取数据列表
     */
    var getList = function () {
        var result;
        // 设置同步
        $.ajaxSetup({
            async: false
        });

        var url = aUrl + "?type=" + type + "&type_id=" + typeId;
        $.post(url, {}, function (data) {
            if (data.success) {
                result = data.data;
            } else {
                //TODO...
            }
        }, 'json');

        // 恢复异步
        $.ajaxSetup({
            async: true
        });
        return result;
    }

    /**
     * 渲染TREE列表
     */
    var treeIns = layui.treeGird({
        elem: '#treeList', //传入元素选择器
        spreadable: true, //设置是否全展开，默认不展开
        checkbox: true,
        nodes: getList(),
        layout: layout
    });
    form.render();

    /**
     * 全部收缩
     */
    $('#collapse').on('click', function () {
        layui.collapse(treeIns);
        return false;
    });

    /**
     * 全部展开
     */
    $('#expand').on('click', function () {
        layui.expand(treeIns);
        return false;
    });

    /**
     * 行选中事件
     */
    $('.btnEdit').on('click', function () {
        var id = $(this).data('id');
        $('#' + id + ' input[type="checkbox"]').prop("checked", 'off');
        $("#" + id + " .layui-form-checkbox").removeClass("layui-form-checked").addClass("layui-form-checked");
        $('#' + id + ' input[type="checkbox"]').prop("checked", 'on');
    });

    /**
     * 取消
     */
    $('.btnEdit2').on('click', function () {
        var id = $(this).data('id');
        $('#' + id + ' input[type="checkbox"]').prop("checked", false);
        $("#" + id + " .layui-form-checkbox").removeClass("layui-form-checked");
    });
    //    

    /**
     * 监听表单并提交
     */
    form.on('submit(submitForm)', function (data) {
        var index = layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.2});
        var url = "/manager/adminauth/setauth";
        $.post(url, data.field, function (data) {
            if (data.success) {
                layer.close(index);
                layer.msg("保存成功！");
                layer.closeAll("iframe");
                history.back();
                return false;
            } else {
                layer.close(index);
                layer.msg(data.msg);
            }
        }, 'json');

        return false;
    });
});